﻿
var gridColCount = 11;
var gridRowCount = 12;
var headRowCount = 3;
var rowColCount = 2;

function testGrid() {
    try {
        var _html = "";

        var rowIndex = 0;
        var $grid = $('#setGrid');

        $('#test-grid').html(_html);

        /*--------------------------------------------------------
        设置列宽
        --------------------------------------------------------*/
        var arr_width = new Array();
        arr_width = getGridWidthJson();

        /*--------------------------------------------------------
        表头
        --------------------------------------------------------*/
        var arr_head = new Array();
        arr_head = getGridHeadJson(arr_width);

        /*--------------------------------------------------------
        固定行设置
        --------------------------------------------------------*/
        var arr_rows = new Array();
        arr_rows = getGridRowJson(arr_width);


        //生成 HTML
        for (var row = 0; row < arr_head.length; row++) {
            _html += "<tr>";
            for (var col = 0; col < arr_head[row].length; col++) {
                if (arr_head[row][col].text != '') {
                    var _strWidth = arr_width[col] > 0 ? ("width:" + arr_width[col] + "px") : "";
                    _html += "<td colspan='" + arr_head[row][col].colspan + "' rowspan='" + arr_head[row][col].rowspan
                          + "' style='text-align:center;" + _strWidth + "'>"
                          + arr_head[row][col].text + "</td>";
                }
            }
            _html += "</tr>";
        }

        for (var row = 0; row < gridRowCount - headRowCount; row++) {
            _html += "<tr>";
            for (var col = 0; col < gridColCount; col++) {
                if (col < rowColCount) {
                    if (arr_rows[row][col].colspan > 0 && arr_rows[row][col].rowspan > 0) {
                        _html += "<td colspan='" + arr_rows[row][col].colspan + "' rowspan='" + arr_rows[row][col].rowspan + "' style='text-align:center'>"
                              + arr_rows[row][col].text + "</td>";
                    }
                } else {
                    _html += "<td><input type='text' value='' style='width:40px'/></td>";
                }
            }
            _html += "</tr>";
        }

        $('#test-grid').html(_html);
    }
    catch (err) {
        alert(err.description);
    }
}

/*-------------------------------------------------
获取JSON
-------------------------------------------------*/
/*获取表格宽度JSON*/
function getGridWidthJson() {
    var $grid = $('#setGrid');
    var result = new Array();

    $grid.find('tr.width').each(function () {
        $(this).find('td:gt(0)').each(function (index) {
            var _text = $(this).find(':text').val();
            result[index] = (_text == "") ? -1 : (_text - 0);
        });
    });
    return result;
}

/*获取表头的JSON*/
function getGridHeadJson(arr_width) {
    var $grid = $('#setGrid');
    var result = new Array();

    $grid.find('tr.head').each(function (rowIndex) {
        result[rowIndex] = new Array();
        $(this).find('td:gt(0)').each(function (colIndex) {
            var _text = $(this).find(':text').val();
            result[rowIndex][colIndex] = { text: _text, colno: colIndex, colspan: 1, rowspan: 1, width: arr_width[colIndex], visible: true };
        });
    });

    //向右合并单元格
    for (var row = 0; row < result.length; row++) {
        var colIndex = -1;
        for (var col = 0; col < result[row].length; col++) {
            if (result[row][col].text == "") {
                var isSpan = true;
                if (row > 0) {
                    for (var iRow = row - 1; (iRow >= 0 && isSpan) ; iRow--) {
                        if (result[iRow][col].text != "") {
                            isSpan = false;
                        }
                    }
                }
                if (isSpan) {
                    if (colIndex > 0) {
                        result[row][colIndex].colspan++;
                        result[row][colIndex].width += result[row][col].width;
                    }
                    result[row][col].colspan = -1;
                }
            }
            else {
                colIndex = col;
            }
        }
    }
    for (var row = 0; row < result.length; row++) {
        for (var col = 0; col < result[row].length; col++) {
            if (result[row][col].text == "") {
                result[row][col].colspan = -1;
            } else {
                break;
            }
        }
    }
    //向上合并单元格、除去右向合并的误差
    for (var row = 0; row < result.length - 1; row++) {
        for (var col = 0; col < result[row].length; col++) {
            var _colspan = result[row][col].colspan;
            var _text = result[row][col].text;
            if (_colspan > 0 && _text != "") {
                var _rowspan = 1;
                for (var iRow = row + 1; iRow < result.length; iRow++) {
                    var _emptyCount = 0;
                    for (var iCol = col; iCol < (col + _colspan) ; iCol++) {
                        if (result[iRow][iCol].text == "") {
                            _emptyCount++;
                        } else {
                            break;
                        }
                    }
                    if (_emptyCount == _colspan) {
                        _rowspan++;
                    } else {
                        break;
                    }
                }
                result[row][col].rowspan = _rowspan;
            }
        }
    }
    return result;
}

/*获取表格固定行JSON*/
function getGridRowJson(arr_width) {
    var $grid = $('#setGrid');
    var arr_rows = new Array();
    $grid.find('tr.row').each(function (rowIndex) {
        $(this).find('td:gt(0)').each(function (colIndex) {
            if ($(this).find(':text').length > 0) {
                var _text = $(this).find(':text').length > 0 ? $(this).find(':text').val() : "";

                if (!arr_rows[rowIndex]) {
                    arr_rows[rowIndex] = new Array();
                }
                arr_rows[rowIndex][colIndex] = { text: _text, colno: colIndex, colspan: 1, rowspan: 1, width: -1, visible: true };
            }
        });
    });

    //向上合并单元格
    for (var col = 0; col < rowColCount; col++) {
        var rowIndex = 0;
        for (var row = 0; row < gridRowCount - headRowCount; row++) {
            if (arr_rows[row][col].text == "") {
                var isSpan = true;
                if (col > 0) {
                    isSpan = true;
                    for (var iCol = col - 1; (iCol >= 0 && isSpan) ; iCol--) {
                        if (arr_rows[row][iCol].text != "") {
                            isSpan = false;
                        }
                    }
                }
                if (isSpan) {
                    arr_rows[rowIndex][col].rowspan++;
                    arr_rows[row][col].rowspan = -1;
                }
                else {
                    rowIndex = row;
                }
            }
            else {
                rowIndex = row;
            }
        }
    }

    //向左合并单元格、除去向上合并的误差
    for (var col = 0; col < rowColCount; col++) {
        for (var row = 0; row < gridColCount - headRowCount; row++) {
            var _rowspan = arr_rows[row][col].rowspan;
            var _text = arr_rows[row][col].text;
            if (_rowspan > 0 && _text != "") {
                var _colspan = 1;
                for (var iCol = col + 1; iCol < rowColCount; iCol++) {
                    var _emptyCount = 0;
                    for (var iRow = row; iRow < (row + _rowspan) ; iRow++) {
                        if (arr_rows[iRow][iCol].text == "") {
                            _emptyCount++;
                        }
                        else {
                            break;
                        }
                    }
                    if (_emptyCount == _rowspan) {
                        _colspan++;
                    }
                    else {
                        break;
                    }
                }
                arr_rows[row][col].colspan = _colspan;
            }
        }
    }

    return arr_rows;
}

function gridInit() {
    gridColCount = 11;
    gridRowCount = 12;
    headRowCount = 3;
    rowColCount = 2;

    $("#txtGridColCount").val(gridColCount);
    $("#txtGridRowCount").val(gridRowCount);
    $("#txtHeadRowCount").val(headRowCount);
    $("#txtRowColCount").val(rowColCount);

    $("#setGrid").html('');
    $("#test-grid").html('');
}


//显示弹出窗2
function showTestItemWindows2(id) {
    $('#hf_GaugeTestId').val(id);
    $('#hf_grid_GaugeTestItemId').val(-1)
    gridInit();
    if (id > 0) {
        $.ajax({
            type: "post",
            datatype: "json",
            url: "GaugeEdit.aspx/GetGaugeTestData",
            contentType: "application/json",
            data: "{Id:" + id + "}",
            success: function (result) {
                var data = result.d;
                if (data != null) {
                    var arr_width = new Array();
                    var arr_head = new Array();
                    var arr_rows = new Array();
                    arr_head = JSON.parse(data.GridValue);
                    var params = JSON.parse(data.GridParams);
                    arr_width = params.arr_width;
                    $('#txtGridColCount').val(params.gridColCount);
                    $('#txtGridRowCount').val(params.gridRowCount);
                    $('#txtHeadRowCount').val(params.headRowCount);
                    $('#txtRowColCount').val(params.rowColCount);

                    gridColCount = params.gridColCount;
                    gridRowCount = params.gridRowCount;
                    headRowCount = params.headRowCount;
                    rowColCount = params.rowColCount;

                    $('#grid_radio tr:not(:first)').remove();
                    $.ajax({
                        type: "post",
                        datatype: "json",
                        url: "GaugeEdit.aspx/GetGaugeTestItemList",
                        contentType: "application/json",
                        data: "{GaugeTestId:" + id + "}",
                        success: function (result) {
                            var data = result.d;
                            if (data.length > 0) {
                                arr_rows = JSON.parse(data[0].Value);
                                $('#hf_grid_GaugeTestItemId').val(data[0].GaugeTestItemId);
                                builderGridHtml(arr_width, arr_head, arr_rows);
                            }
                        }
                    });
                }
            }
        });
    }
    $('#winTestItem2').window('open', {
        title: '新增'
    });
}

function showGridSetting() {

    gridColCount = $('#txtGridColCount').val();
    gridRowCount = $('#txtGridRowCount').val();
    headRowCount = $('#txtHeadRowCount').val();
    rowColCount = $('#txtRowColCount').val();

    var $grid = $('#setGrid');
    var _html = "";
    _html += "<tr>";
    _html += "<th style='width:80px'>序号</th>";
    for (var col = 1; col <= gridColCount; col++) {
        _html += "<th>列" + col + "</th>";
    }
    _html += "</tr>";
    //设置宽
    _html += "<tr class='width'>";
    _html += "<td style='width:80px'>设置宽</td>";

    for (var col = 1; col <= gridColCount ; col++) {
        _html += "<td><input type='text' value='100' style='width:60px'><span>px</span></td>";
    }
    _html += "</tr>";
    //设置表头
    for (var row = 1; row <= headRowCount; row++) {
        _html += '<tr class="head">';
        _html += "<td>表头 " + row + "</td>";
        for (var col = 1; col <= gridColCount; col++) {
            _html += "<td><input type='text' value='' style='width:60px'></td>";
        }
        _html += '</tr>';
    }
    //设置行
    for (var row = 1; row <= gridRowCount - headRowCount; row++) {
        _html += "<tr class='row'>";
        _html += "<td>行 " + row + "</td>";
        for (var col = 0; col < gridColCount; col++) {
            if (col < rowColCount) {
                _html += "<td><input type='text' value='' style='width:60px'></td>";
            } else {
                _html += "<td>{输入框}</td>";
            }
        }
        _html += '</tr>';
    }

    $grid.html(_html);
}

function builderGridHtml(arr_width, arr_head, arr_rows) {
    var _html = "";
    _html += "<tr>";
    _html += "<th style='width:80px'>序号</th>";
    for (var col = 0; (arr_head.length > 0 && col < arr_head[0].length) ; col++) {
        _html += "<th>列" + (col + 1) + "</th>";
    }
    _html += "</tr>";
    //设置宽
    _html += "<tr class='width'>";
    _html += "<td style='width:80px'>设置宽</td>";
    for (var col = 0; col < arr_width.length ; col++) {
        _html += "<td><input type='text' value='" + arr_width[col] + "' style='width:60px'><span>px</span></td>";
    }
    _html += "</tr>";
    //设置表头
    for (var row = 0; row < arr_head.length; row++) {
        _html += '<tr class="head">';
        _html += "<td>表头 " + row + "</td>";
        for (var col = 0; col < arr_head[row].length; col++) {
            _html += "<td><input type='text' value='" + arr_head[row][col].text + "' style='width:60px'></td>";
        }
        _html += '</tr>';
    }
    //设置行
    for (var row = 0; row < arr_rows.length; row++) {
        _html += "<tr class='row'>";
        _html += "<td>行 " + (row + 1) + "</td>";
        for (var col = 0; col < arr_head[0].length; col++) {
            if (col < arr_rows[row].length) {
                _html += "<td><input type='text' value='" + arr_rows[row][col].text + "' style='width:60px'></td>";
            } else {
                _html += "<td>{输入框}</td>";
            }
        }
        _html += '</tr>';
    }

    var $grid = $('#setGrid');
    $grid.html(_html);
}

function setRow() {
    var gridColCount = $('#txtRowColCount').val();
    var gridRowCount = $('#txtRowRowCount').val();
    var $grid = $('#grid_row_edit');
    var _html = "";
    _html += "<tr>";
    _html += "<th>序号</th>";
    for (var col = 1; col <= gridRowCount; col++) {
        _html += "<th>行" + col + "</th>";
    }
    _html += "</tr>";
    for (var row = 1; row <= gridColCount; row++) {
        _html += '<tr>';
        _html += "<td>" + row + "</td>";
        for (var col = 1; col <= gridRowCount; col++) {
            _html += "<td><input type='text' value='' style='width:60px'></td>";
        }
        _html += '</tr>';
    }
    $grid.html(_html);
}

$(function () {

});


/*-----------------------------------------------
保存
-----------------------------------------------*/
function saveGridData() {

    var arr_width = new Array();
    arr_width = getGridWidthJson();

    var arr_head = new Array();
    arr_head = getGridHeadJson(arr_width);

    var arr_rows = new Array();
    arr_rows = getGridRowJson(arr_width);

    var GaugeTestId = $('#hf_GaugeTestId').val();
    var GridParams = {
        gridColCount: gridColCount,
        gridRowCount: gridRowCount,
        headRowCount: headRowCount,
        rowColCount: rowColCount,
        arr_width: arr_width
    }
    var model = {
        GaugeId: GaugeId,
        GaugeTestId: GaugeTestId,
        Title: '步态分析表',
        GridValue: JSON.stringify(arr_head),
        GridParams: JSON.stringify(GridParams),
        TestType: 'STEP_GRID'
    };
    var list = new Array();

    var item_model = new Object();
    item_model.GaugeTestItemId = $('#hf_grid_GaugeTestItemId').val();
    item_model.Value = JSON.stringify(arr_rows);
    item_model.Score = 0;
    item_model.ItemTypeNo = "STEP_GRID";
    list[list.length] = item_model;

    var _data = { JsonModel: JSON.stringify(model), JsonList: JSON.stringify(list) };
    $.ajax({
        type: "post",
        datatype: "json",
        url: "GaugeEdit.aspx/SaveGaugeTestDate",
        contentType: "application/json",
        data: JSON.stringify(_data),
        success: function (result) {
            if (result.d > 0) {
                alert("保存操作成功！");
                $('#winTestItem2').window('close');
            }
            else {
                alert("操作失败！");
            }
        }
    });
}



